﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using vuonthieunhi.Library;

namespace vuonthieunhi.Models
{
    public class CategoryDao:BaseDao
    {
        public List<Category> GetList()
        {
            return dbContext.Categories.OrderBy(c => c.CategoryOrder).ToList<Category>();
        }

        public Message Insert(Category objUI)
        {
            Message msg = null;
            try
            {
                if (objUI != null)
                {
                        objUI.Active = true;
                        dbContext.Categories.InsertOnSubmit(objUI);
                        dbContext.SubmitChanges();

                        // Show success message
                        msg = new Message(MessageConstants.I0001, MessageType.Info, "Danh mục '" + objUI.CategoryName + "'", "thêm");
                }
            }
            catch (Exception)
            {
                // Show system error
                msg = new Message(MessageConstants.E0007, MessageType.Error);
            }
            return msg;
        }

        public Message Update(Category objUI)
        {
            Message msg = null;
            try
            {
                if (objUI != null)
                {
                    // Get current group in dbContext
                    Category objDb = GetByID(objUI.ID);

                    if (objDb != null)
                    {
                        //if (CheckParentCatIsUsing(objUI))
                        //{
                        //    // Show success message
                        //    Category parent = GetByID(objUI.ParentId.Value);
                        //    msg = new Message(MessageConstants.E0044, MessageType.Error, "'" + parent.CategoryName + "'");
                        //}
                        //else
                        //{
                        // Update info by objUI
                        objDb.Image = objUI.Image;
                        objDb.CategoryName = objUI.CategoryName;
                        objDb.Description = objUI.Description;
                        objDb.ParentId = objUI.ParentId;

                        // Submit changes to dbContext
                        dbContext.SubmitChanges();
                        // Show success message
                        msg = new Message(MessageConstants.I0001, MessageType.Info, "Danh mục '" + objUI.CategoryName + "'", "cập nhật");
                        //}                        
                    }
                }
            }
            catch (Exception)
            {
                // Show system error
                msg = new Message(MessageConstants.E0007, MessageType.Error);
            }

            return msg;
        }

        private Category GetByID(int id)
        {
            return dbContext.Categories.Where(q => q.ID == id).FirstOrDefault();
        }

        public Category GetByAlias(string alias)
        {
            return dbContext.Categories.Where(q => q.Alias == alias && q.Active).FirstOrDefault();
        }

        public List<GetCategoryResult> GetCategoryByPartnerID(int category)
        {
            Category obj = GetByID(category);
            if (obj.ParentId.HasValue)
            {
                return dbContext.GetCategory(obj.ParentId).ToList();
            }
            else
            {
                return dbContext.GetCategory(0).ToList();
            }
        }

        public List<GetCategoryResult> GetCategoryByParentID(int parent)
        {
            return dbContext.GetCategory(parent).ToList();
        }
    }
}